var http = require('http'), fs = require('fs');

function serverStaticFile(res, path, contentType, responseCode) {
    if (!responseCode) {
        responseCode = 200;
    }
    fs.readFile(__dirname + path, function(err, data) {
        if (err) {
            res.writeHead(500, { 'Content-type': 'text/plain' });
            res.end('500 - Internal Error');
        } else {
            res.writeHead(responseCode, { 'Content-type': contentType });
            res.end(data);
        }
    })
}

http.createServer(function(req, res) {
    var path = req.url.replace(/\/?(?:\?.*)?$/, '').toLowerCase();
    switch (path) {
        case '':
            // res.writeHead(200, { 'Content-type': 'text/plain'});
            // res.end('Home page');
            serverStaticFile(res, '/public/home.html', 'text/html');
            break;
        case '/about':
            // res.writeHead(200, { 'Content-type': 'text/plain'});
            // res.end('About');
            serverStaticFile(res, '/public/about.html', 'text/html');
            break
        case '/img/logo.png':
            var imgPath = '/public' + path
                serverStaticFile(res, imgPath, 'image/png');
            break
        default:
            // res.writeHead(404, { 'Content-type': 'text/plain'});
            // res.end('Not Found');
            serverStaticFile(res, '/public/notfound.html', 'text/html', 404);
            break
    }

}).listen(3200);
console.log('Server started on localhost:3200; press Ctrl-C to terminate---')